package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.Bumenguanli;
import com.qf.pojo.Permsetting;
import com.qf.pojo.Usercollection;
import com.qf.service.DeptService;
import com.qf.service.PermsettingService;
import com.qf.service.UsercollectionService;
import com.qf.utils.form.PersonnerManageForm;
import com.qf.utils.vo.PersonnelDataVO;
import com.qf.utils.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/21 0021
 * Author->DT
 * qmxc
 * 行到水穷处 坐看云起时
 * 模块：人事模块Controller层
 */
@Controller
@RequestMapping("/personnel")
public class UsercollectionController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private UsercollectionService usercollectionService;
    @Autowired
    private PermsettingService permsettingService;


    //___________________________人事档案管理_______________________________
    //跳转到人事档案管理页面
    @RequestMapping("/manage")
    public String personnerManage(@RequestParam(defaultValue = "1")Integer index,
                                  @RequestParam(defaultValue = "3")Integer page, Model model,
                                  PersonnerManageForm form){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", page);
        //form是查询条件的封装类的对象
        map.put("form", form);
        //1.查询人事档案数据
        PageInfo<PersonnelDataVO> pageInfo=usercollectionService.findPersonnelInfoByLimit(map);
        //2.将查询到的信息放入Request域中
        //2.1 pageInfo对象
        model.addAttribute("pageInfo", pageInfo);
        //2,2查询条件回显信息
        model.addAttribute("username", form.getUsername());
        model.addAttribute("status", form.getStatus());
        model.addAttribute("checkStatus", form.getCheckStatus());
        model.addAttribute("beginTime", form.getBeginTime());
        model.addAttribute("endTime", form.getEndTime());
        return "pmag/demo1/list";
    }

    //跳转到档案登记页
    @RequestMapping("/add-ui")
    public String addUI(Model model){
        //查询出全部部门信息，放入请求域
        List<Bumenguanli> deptList = deptService.findAll();
        model.addAttribute("deptList", deptList);
        return "pmag/demo1/add";
    }

    //档案登记(新增)
    @PostMapping("/add")
    public String add(Usercollection usercollection) throws IOException {
//        //1.设置文件保存的目录
//        String path = request.getRealPath("/doc");
//
//        //2.利用MultipartFile对象的transferTo方法保存文件
//        if (!file.isEmpty()){
//            file.transferTo(new File(path+"/"+file.getOriginalFilename()));
//            //3.设置档案信息中的附件为文件保存路径
//            usercollection.setAttachment("http://localhost:8080"+path+"/"+file.getOriginalFilename());
//        }
        //仅为添加功能
        int count = usercollectionService.insert(usercollection);
        if (count==1){
            //新增成功,跳转到管理页面
            return "redirect:/personnel/manage";
        }else {
            //新增失败，跳转到登记页面
            return "redirect:/personnel/add-ui";
        }
    }

    //档案添加并将审核状态设置为审核中
    @PostMapping("/add-checking")
    @ResponseBody
    public ResultVO addAndChecking(Usercollection usercollection) throws IOException {
        int count = usercollectionService.insertAndSetCheckState(usercollection);
        if (count==1){
            //新增并设置状态成功,跳转到管理页面
            return new ResultVO(0, "提交审核成功！", null);
        }else {
            //新增并设置状态失败，跳转到登记页面
            return new ResultVO(0, "提交审核失败！", null);

        }
    }

    //修改信息并将审核状态设置为审核中
    @PostMapping("/set-checking")
    @ResponseBody
    public ResultVO setChecking(Usercollection usercollection) throws IOException {
        int count = usercollectionService.setCheckState(usercollection);
        if (count==1){
            //修改并设置状态成功,跳转到管理页面
            return new ResultVO(0, "提交审核成功！", null);
        }else {
            //修改并设置状态失败，跳转到登记页面
            return new ResultVO(0, "提交审核失败！", null);

        }
    }


    //档案明细
    @RequestMapping("/info")
    public String info(Integer id,Model model){
        //根据档案id查询档案信息
        Usercollection personnel = usercollectionService.selectByPrimaryKey(id);
        //数据放到请求域中
        model.addAttribute("info", personnel);
        //跳转页面
        return "pmag/demo1/view";
    }

    //跳转档案修改页面
    @GetMapping("/update-ui")
    public String updateUI(Integer id,Model model){
        //根据档案id查询档案信息
        Usercollection personnel = usercollectionService.selectByPrimaryKey(id);
        //查询出全部部门信息
        List<Bumenguanli> deptList = deptService.findAll();
        //根据用户的部门id查询出该部门全部职位信息
        List<Permsetting> roleList = permsettingService.findAllRolesByDeptId(personnel.getAppartment());
        //将数据放到请求域中去
        model.addAttribute("deptList", deptList);
        model.addAttribute("personnel", personnel);
        model.addAttribute("roleList", roleList);

        return "pmag/demo1/update";
    }

    //档案修改
    @PostMapping("/update")
    public String update(Usercollection usercollection) throws IOException {

        //修改
        System.out.println(usercollection);
        int count = usercollectionService.updateByPrimaryKeySelective(usercollection);
        if (count==1){
            //新增成功,跳转到管理页面
            return "redirect:/personnel/manage";
        }else {
            //新增失败，跳转到登记页面
            return "redirect:/personnel/update-ui?id="+usercollection.getId();
        }
    }

    //档案逻辑删除
    @GetMapping("/delete")
    @ResponseBody
    public ResultVO delete(Integer id, HttpServletResponse response) throws IOException {
        //删除:根据id修改档案状态为已删除
        int count=usercollectionService.updateCheckStateToDelete(id);
        if (count==1){
            return new ResultVO(0, "", null);
        }else {
            return new ResultVO(44, "", null);
        }
        //给用户提示
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = response.getWriter();
//        if (count==1){
//            //删除成功
//            writer.print("<script>alert('删除成功'); location.href='/personnel/manage'</script>");
//        }
//        else {
//            //删除失败
//            writer.print("<script>alert('删除失败'); location.href='/personnel/manage'</script>");
//
//        }

    }

    //跳转到档案变更页面
    @GetMapping("/modify-ui")
    public String modifyUI(Integer id,Model model){
        //根据档案id查询档案信息
        Usercollection personnel = usercollectionService.selectByPrimaryKey(id);
        //查询出全部部门信息
        List<Bumenguanli> deptList = deptService.findAll();
        //根据用户的部门id查询出该部门全部职位信息
        List<Permsetting> roleList = permsettingService.findAllRolesByDeptId(personnel.getAppartment());
        //将数据放到请求域中去
        model.addAttribute("deptList", deptList);
        model.addAttribute("personnel", personnel);
        model.addAttribute("roleList", roleList);

        return "pmag/demo1/modify";
    }

    //审核通过的档案进行变更(修改信息+修改审核状态为变更)
    @PostMapping("/modify")
    public String modify(Usercollection usercollection) throws IOException {
        //保存变更信息
        int count = usercollectionService.modify(usercollection);
        if (count==1){
            //保存变更信息成功,跳转到管理页面
            return "redirect:/personnel/manage";
        }else {
            //保存变更信息失败，跳转到变更页面
            return "redirect:/personnel/modify-ui?id="+usercollection.getId();
        }
    }


    //审核通过的档案进行变更并提交申请(修改信息+修改审核状态为变更审核中)
    @PostMapping("/modify-checking")
    @ResponseBody
    public ResultVO modifyChecking(Usercollection usercollection) throws IOException {
        //变更提交
        int count = usercollectionService.modifyAndChecking(usercollection);
        if (count==1){
            //提交变更成功,跳转到管理页面
            return new ResultVO(0, "提交审核成功！", null);
        }else {
            //提交变更失败，跳转到变更页面
            return new ResultVO(0, "提交审核失败！", null);

        }
    }


    //___________________________人事档案审批_______________________________
    //跳转到人事档案审批页面
    @RequestMapping("/check")
    public String personnerCheck(@RequestParam(defaultValue = "1")Integer index,
                                 @RequestParam(defaultValue = "3")Integer page, Model model,
                                 PersonnerManageForm form){

        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", page);
        //form是查询条件的封装类的对象
        map.put("form", form);
        //1.查询人事档案数据
        PageInfo<PersonnelDataVO> pageInfo=usercollectionService.findPersonnelInfoByLimitForCheck(map);
        //2.将查询到的信息放入Request域中
        //2.1 pageInfo对象
        model.addAttribute("pageInfo", pageInfo);
        //2,2查询条件回显信息
        model.addAttribute("username", form.getUsername());
        model.addAttribute("beginTime", form.getBeginTime());
        model.addAttribute("endTime", form.getEndTime());
        return "pmag/demo1/list2";
    }

    //跳转到审核档案页面
    @GetMapping("/edit-ui")
    public String editUI(Integer id,Model model){
        //根据档案id查询档案信息
        Usercollection personnel = usercollectionService.selectByPrimaryKey(id);
        //数据放到请求域中
        model.addAttribute("info", personnel);
        return "pmag/demo1/edit";
    }

    //审核档案通过
    @GetMapping("/edit-pass")
    @ResponseBody
    public ResultVO editPass(Integer id){
        //根据id修改档案的审核状态
        int count=usercollectionService.updateCheckStateToPass(id);
        if (count==1){
            //修改成功
            return new ResultVO(0,"",null);
        }
        else {
            //修改失败
            return new ResultVO(44,"",null);
        }
    }

    //审核档案驳回
    @GetMapping("/edit-back")
    @ResponseBody
    public ResultVO editBack(Integer id){
        //根据id修改档案的审核状态
        int count=usercollectionService.updateCheckStateToBack(id);
        if (count==1){
            //修改成功
            return new ResultVO(0,"",null);
        }
        else {
            //修改失败
            return new ResultVO(44,"",null);
        }
    }

    //已删除档案恢复
    @GetMapping("/delete-recover")
    public void deleteRecover(Integer id, HttpServletResponse response) throws IOException {
        //删除恢复:根据id修改档案状态为试用期;
        int count=usercollectionService.recoverDeleteState(id);
        //给用户提示
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (count==1){
            //删除成功
            writer.print("<script>alert('档案恢复成功'); location.href='/personnel/check'</script>");
        }
        else {
            //删除失败
            writer.print("<script>alert('档案恢复失败'); location.href='/personnel/check'</script>");

        }
    }

    //逻辑删除的档案进行永久删除
    @GetMapping("/delete-forever")
    public void deleteForever(Integer id, HttpServletResponse response) throws IOException {
        //永久删除
        int count=usercollectionService.foreverDelete(id);
        //给用户提示
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (count==1){
            //删除成功
            writer.print("<script>alert('永久删除档案成功'); location.href='/personnel/check'</script>");
        }
        else {
            //删除失败
            writer.print("<script>alert('永久删除档案失败'); location.href='/personnel/check'</script>");

        }
    }

    //跳转档案复核页面
    @GetMapping("/recheck-ui")
    public String recheckUI(){
        return "pmag/demo1/recheck";
    }
}
